package utils

import (
	"fmt"
	"time"
)

// TimeAsDate 时间戳转时间格式
func TimeAsDate(times int64) string {
	// 时间戳转日期
	Loc, _ := time.LoadLocation("Asia/Shanghai")
	if times != 0 {
		t := time.Unix(times, 0).In(Loc)
		return t.Format(time.DateTime)
	} else {
		t := time.Now().In(Loc)
		return t.Format(time.DateTime)
	}
}

// DateAsTime 时间格式转时间戳
func DateAsTime(dates string) int64 {
	// 日期准时间戳
	Loc, _ := time.LoadLocation("Asia/Shanghai")
	if dates != "" {
		t2, _ := time.ParseInLocation(time.DateTime, dates, Loc)
		return t2.Unix()
	} else {
		t := time.Now().In(Loc)
		return t.Unix()
	}
}

// TimeOrDateAsYear 日期或时间戳转年
func TimeOrDateAsYear(unix int64, date string, format string) string {
	Loc, _ := time.LoadLocation("Asia/Shanghai")
	var t time.Time
	if unix != 0 {
		t = time.Unix(unix, 0).In(Loc)
	} else if date != "" {
		t, _ = time.ParseInLocation(time.DateTime, date, Loc)
	} else {
		t = time.Now().In(Loc)
	}
	var Month, Day, Hour, Minute, Second string
	if t.Month() < 10 {
		Month = fmt.Sprintf("0%d", t.Month())
	} else {
		Month = fmt.Sprintf("%d", t.Month())
	}
	if t.Day() < 10 {
		Day = fmt.Sprintf("0%d", t.Day())
	} else {
		Day = fmt.Sprintf("%d", t.Day())
	}
	if t.Hour() < 10 {
		Hour = fmt.Sprintf("0%d", t.Hour())
	} else {
		Hour = fmt.Sprintf("%d", t.Hour())
	}
	if t.Minute() < 10 {
		Minute = fmt.Sprintf("0%d", t.Minute())
	} else {
		Minute = fmt.Sprintf("%d", t.Minute())
	}
	if t.Second() < 10 {
		Second = fmt.Sprintf("0%d", t.Second())
	} else {
		Second = fmt.Sprintf("%d", t.Second())
	}
	switch format {
	case "Y":
		return fmt.Sprintf("%d", t.Year())
	case "m":
		return Month
	case "d":
		return Day
	case "H":
		return Hour
	case "i":
		return Minute
	case "s":
		return Second
	case "Ym":
		return fmt.Sprintf("%d%s", t.Year(), Month)
	case "Ymd":
		return fmt.Sprintf("%d%s%s", t.Year(), Month, Day)
	case "YmdHis":
		return fmt.Sprintf("%d%s%s%s%s%s", t.Year(), Month, Day, Hour, Minute, Second)
	case "Y-m":
		return fmt.Sprintf("%d-%s", t.Year(), Month)
	case "Y-m-d":
		return fmt.Sprintf("%d-%s-%s", t.Year(), Month, Day)
	case "H:i:s":
		return fmt.Sprintf("%s:%s:%s", Hour, Minute, Second)
	default:
		return fmt.Sprintf("%s", t.Format(time.DateTime))
	}
}

func TimeCount(timeType string) (int64, int64) {
	Loc, _ := time.LoadLocation("Asia/Shanghai")
	t := time.Now().In(Loc)
	var star, end time.Time
	// 今日日期
	switch timeType {
	/*case "Day":
	star = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
	end = star.AddDate(0, 0, 1)
	break*/
	case "LastDay":
		end = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
		star = end.AddDate(0, 0, -1)
		break
	case "Week":
		today := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
		star = today.AddDate(0, 0, -int(today.Weekday())+1)
		end = star.AddDate(0, 0, 7)
		break
	case "LastWeek":
		today := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
		end = today.AddDate(0, 0, -int(today.Weekday())+1)
		star = end.AddDate(0, 0, -7)
		break
	case "Month":
		star = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
		end = star.AddDate(0, 1, 0)
		break
	case "LastMonth":
		end = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
		star = end.AddDate(0, -1, 0)
		break
	case "Year":
		star = time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.Local)
		end = star.AddDate(1, 0, 0)
		break
	case "LastYear":
		end = time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.Local)
		star = end.AddDate(-1, 0, 0)
		break
	default:
		star = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
		end = star.AddDate(0, 0, 1)
		break
	}
	return star.Unix(), end.Unix()
}
